package com.ufo.store.management.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.ufo.store.management.R;

/**
 * Created by taoli on 2016/10/18 0018.
 */

public class TipsDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private OnItemClick mListener = null;
    private TextView mTitleTv;
    private String mTips;

    public TipsDialog(Context context) {
        super(context, R.style.select_dialog);
        this.context = context;
    }

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_tips);
        mTitleTv = (TextView) findViewById(R.id.tv_title);
        findViewById(R.id.tv_ok).setOnClickListener(this);
        if (!TextUtils.isEmpty(mTips)) {
            mTitleTv.setText(mTips);
        }
    }

    public void setTips(String s) {
        mTips = s;
        if (mTitleTv != null && !TextUtils.isEmpty(mTips)) {
            mTitleTv.setText(mTips);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_ok:
                if (mListener != null)
                    mListener.onItemClick(0);
                break;
        }
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (null != TipsDialog.this && TipsDialog.this.isShowing()) {
                    TipsDialog.this.dismiss();
                }
            }
        }, 100);
    }

    @Override
    public void setTitle(CharSequence title) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void show() {
        if (context != null && ((Activity) context).isFinishing()) {
            return;
        }
        super.show();
    }

    public void setItemClickListener(OnItemClick p) {
        mListener = p;
    }

    public interface OnItemClick {
        public void onItemClick(int position);
    }
}

